/* 
 * File:   posixcpp_bytearray_test.cpp
 * Author: starling13
 *
 * Created on 29.01.2015, 15:53:11
 */

#include <stdlib.h>
#include <iostream>

#include "posixcpp_bytearray.hpp"

using namespace posixcpp;

ByteArray *ba[5];
unsigned char buf[] = "absdefgh";

void constructors( )
{
	std::cout << "posixcpp_bytearray_test constructors" << std::endl;
	ba[0] = new ByteArray;
	ba[1] = new ByteArray(16, 0xFF);
	ba[2] = new ByteArray(buf, sizeof(buf));
	ba[2]->clear();
	ba[3] = new ByteArray(buf, sizeof(buf));
	ba[4] = new ByteArray(*ba[1]);
	ba[0]->wrapExistingData(buf, sizeof(buf));
}

void printing ( ) {
	std::cout << ba[0]->toHex(8) << std::endl;
	std::cout << ba[1]->toHex(8) << std::endl;
	std::cout << ba[2]->toHex(8) << std::endl;
	std::cout << ba[3]->toHex(8) << std::endl;
	std::cout << ba[4]->toHex(8) << std::endl;
}

void resizing() {
	ba[1]->reserve(255);
	ba[2]->resize(128);
	ba[3]->copyExistingData(buf, sizeof(buf));
	if ( ByteArray::wrapper(ba[3]->data())!=ba[3] )
		throw;
	ba[4]->fill(0x3,12);
}

void destructors( )
{
	std::cout << "posixcpp_bytearray_test destructors" << std::endl;
	delete ba[0];
	delete ba[1];
	delete ba[2];
	delete ba[3];
	delete ba[4];
}

int main( int argc, char** argv )
{
	std::cout << "%SUITE_STARTING% posixcpp_bytearray_test" << std::endl;
	std::cout << "%SUITE_STARTED%" << std::endl;

	std::cout << "%TEST_STARTED% constructors (posixcpp_bytearray_test)" << std::endl;
	constructors();
	std::cout << "%TEST_FINISHED% time=0 constructors (posixcpp_bytearray_test)" << std::endl;

	std::cout << "%TEST_STARTED% destructors (posixcpp_bytearray_test)\n" << std::endl;
	printing();
	std::cout << "%TEST_FINISHED% time=0 destructors (posixcpp_bytearray_test)" << std::endl;
	
	std::cout << "%TEST_STARTED% resizing (posixcpp_bytearray_test)\n" << std::endl;
	resizing();
	std::cout << "%TEST_FINISHED% time=0 resizing (posixcpp_bytearray_test)" << std::endl;
	
	std::cout << "%TEST_STARTED% destructors (posixcpp_bytearray_test)\n" << std::endl;
	destructors();
	std::cout << "%TEST_FINISHED% time=0 destructors (posixcpp_bytearray_test)" << std::endl;

	std::cout << "%SUITE_FINISHED% time=0" << std::endl;
	
	return (EXIT_SUCCESS);
}
